Ext
		.define(
				'Service.com.controller.ComController',
				{
					extend : 'WebOs.ex.DefaultController',

					views : [ 'Service.com.view.InitContainer',
							'Service.com.view.ComDetailPanel' ],

					stores : [ 'Service.com.store.HtComStore',
							'Service.com.store.HtBuildStore',
							'Service.com.store.HtPropStore'

					],

					models : [ 'Service.com.model.HtComModel',
							'Service.com.model.HtBuildModel',
							'Service.com.model.HtPropModel',
							'Service.com.model.ComStatModel'

					],

					refs : [
					// getter setter
					],
					
					selectedCom : null,

					init : function() {
						var me = this;
						this.control({
							'combo[module=com]' : {
								select : me.onComComboSelectFire
							},
							'button[id=Service_com_view_basicModifyBtn]' : {
								click : me.onBasicModifyBtnClickFire
							},
							'button[id=Service_com_view_InfoModifyBtn]' : {
								click : me.onInfoModifyBtnClickFire
							},
							'button[id=Service_com_view_ComDelBtn]' : {
								click : me.onComDelBtnClickFire
							},
							'button[id=Service_com_view_UserBtn]' : {
								click : me.onUserBtnClickFire
							},
							'button[id=Service_com_view_ImpPosBtn]' : {
								click : me.onImpPosBtnClickFire
							}
						});
					},
					
					onImpPosBtnClickFire : function(btn){
						if(this.selectedCom){
							core.display('Service_impPos',{condition : {areaId : this.selectedCom}});
						}
					},
					
					onUserBtnClickFire : function(btn){
						if(this.selectedCom){
							core.display('Service_comUser',{condition : {areaId : this.selectedCom}});
						}
					},
					
					onComDelBtnClickFire : function(btn){
						var me = this;
						if(me.selectedCom){
							me.request({
								params : {
									module : 'Service_com',
									action : 'delHtCom',
									id : me.selectedCom
								},
								onSuccess : function(resp){
									Ext.Msg.alert('提示','删除成功');
									var combo = btn.up('toolbar').down('combo');
									var value = combo.getValue();
									var selected = null;
									combo.getStore().each(function(record){
										if(record.get('id') == value){
											selected = record;
										}
									});
									combo.getStore().remove(selected);
									combo.select('');
									me.selectedCom = null;
									me.resetAllValues();
								}
							});
						}else{
							Ext.Msg.alert('提示','没有选中小区');
						}
					},
					
					resetAllValues : function(){
						Ext.getCmp('Service_com_view_basicForm').getForm().reset();
						Ext.getCmp('Service_com_view_StatForm').getForm().reset();
						Ext.getCmp('Service_com_view_InfoForm').getForm().reset();
						document.getElementById('com_preview_img').src='resources/imgs/blank.png';
					},
					
					onInfoModifyBtnClickFire : function(btn){
						var me = this;
						var form = Ext.getCmp('Service_com_view_InfoForm').getForm();
						if(form.isValid() && me.selectedCom){
							var values = form.getValues();
							me.request({
								params : Ext.apply(values,{
									module : 'Service_com',
									action : 'chgComInfo',
									id : me.selectedCom,
								}),
								onSuccess : function(resp){
									if(resp.success){
										Ext.Msg.alert('提示','修改成功');
									}
								}
							});
						}
					},
					
					onBasicModifyBtnClickFire : function(btn){
						var me = this;
						var form = Ext.getCmp('Service_com_view_basicForm').getForm();
						if(form.isValid() && me.selectedCom){
							var values = form.getValues();
							me.request({
								params : Ext.apply(values,{
									module : 'Service_com',
									action : 'chgComBasic',
									id : me.selectedCom,
								}),
								onSuccess : function(resp){
									if(resp.success){
										Ext.Msg.alert('提示','修改成功');
									}
								}
							});
						}
						
					},

					onComComboSelectFire : function(cmp) {
						var value = cmp.getValue();
						this.selectedCom = value;
						if (value) {
							this.queryComDetail(value);
						}
					},

					queryComDetail : function(comId) {
						var me = this;
						me.request({
							params : {
								module : 'Service_com',
								action : 'getHtComStat',
								id : comId
							},
							onSuccess : function(resp) {
								var result = resp.result;
								var record = Ext.create('Service.com.model.ComStatModel');
								record.set('userCount',result.userCount);
								record.set('impUserCount',result.impUserCount);
								record.set('impPosCount',result.impPosCount);
								record.set('carCount',result.carCount);
								Ext.getCmp('Service_com_view_StatForm').loadRecord(record);
							}
						});
						me.request({
									params : {
										module : 'Service_com',
										action : 'getHtComById',
										id : comId
									},
									onSuccess : function(resp) {
										var record = Ext
												.create('Service.com.model.HtComModel');
										var result = resp.result;
										record.set('name', result.name);
										record.set('propId',result.propId);
										record.set('propName', result.propName);
										record.set('area', result.area);
										record.set('maxFloor', result.maxFloor);
										record.set('buildNum', result.buildNum);
										Ext.getCmp(
														'Service_com_view_basicForm')
												.loadRecord(record);
										Ext.getCmp('Service_com_view_InfoForm')
												.down('textareafield')
												.setValue(result.info);
										document
												.getElementById('com_preview_img').src = 'resources/imgs/com/'
												+ result.img;
									}
								});
					}
				});
